<?php
$target_dir= "./upload";
$file_name = $_FILES["file"]["name"];

//Definition of variables
$AMRFile = $file_name;
$pieces = explode(".", $AMRFile);
$WAVFile = $pieces[0] . ".wav";

$target_file_full_path = $target_dir . "/" . $AMRFile;
                          
// upload file to tmp file
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_full_path);

// system script to convert AMR to WAV
$x = $target_dir . "/" . $AMRFile;
$y = $target_dir . "/" . $WAVFile;
//convertAmrToWav($AMRFile, $WAVFile); 
convertAmrToWav($x, $y);

// download WAV content
//downloadFile($target_file_full_path);
//downloadFile("C:/PortableApps/xampplite/htdocs/audio/1500Hz.wav");
downloadFile($target_dir . "/" . $WAVFile);

 /********************** FUNCTIONS **********************/
 
function downloadFile($filePath){
  header ('Content-Type: application/octet-stream');
  header ('Accept-Ranges: bytes');
  header ('Content-Length: '. filesize($filePath));
  header ("Content-Disposition: attachment; filename=logo.png");
  ob_clean();
  flush();
  readfile($filePath);
}

function convertAmrToWav($AMRFile, $WAVFile){
//ffmpeg command   --  convert from AMR to WAV
$command = "ffmpeg -i $AMRFile $WAVFile";
//$command = "rename " . $pathToAMRFile .  " " . $wav_file;
shell_exec($command);

}
             
?>